package com.yjtc.cbg.commonutildemo.View;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.yjtc.cbg.commonutildemo.R;

/**
 * Title: CommonUtilDemo
 * <p>
 * Description:
 * <p>
 * Author:baigege (baigegechen@gmail.com)
 * <p>
 * Date:2017-05-16
 */
public class PreviewCameraActivity extends BaseActivity implements View.OnClickListener {


    public static final int PREMISSION_REQUEST_CAMARE = 454;
    private static String PREMISSION_CAMARE = "Manifest.premission.CAMERA";

    private TextView tv_preview;

    @Override
    public int getContentVIew() {
        return R.layout.activity_preview_camera;
    }

    @Override
    public void initData() {
        super.initData();
        tv_preview = (TextView) findViewById(R.id.tv_preview);
        tv_preview.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

    }

    private void checkPremission() {
        if (ContextCompat.checkSelfPermission(mContext, PREMISSION_CAMARE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{PREMISSION_CAMARE}, PREMISSION_REQUEST_CAMARE);
        } else {
            startWallPaper();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PREMISSION_REQUEST_CAMARE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    startWallPaper();
                } else {
                    Toast.makeText(mContext,"全县获取失败",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void startWallPaper() {
        Intent wallpaperIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
        Intent choose = Intent.createChooser(wallpaperIntent, getString(R.string.app_name));
        startActivity(choose);
    }
}
